import store from "@/store";
import { Toast } from 'vant';

export default function () {
    return new Promise((resolve, reject) => {
        //页面刷新查找储存用户信息
        let token = localStorage.getItem("token");
        let tokenExpired = localStorage.getItem("tokenExpired");
        // 判断是否登陆过
        if (token) {
            //登录过
            //获取当前时间
            let now = new Date().getTime();
            //判断 身份验证时间是否过期
            if (now >= Number(tokenExpired)) {
                localStorage.removeItem("token");
                localStorage.removeItem("tokenExpired");
                Toast('获取登录信息失败，请重新登录')
                resolve();
            } else {
                Toast.loading({
                    message: '加载中...',
                    forbidClick: true,
                });

                //请求接口
                store.dispatch('getUserInfo', token).then(res => {
                    resolve();
                    Toast.clear()
                }).catch(err => {
                    Toast('网络异常请重新加载')
                    reject();
                })
            }
        } else {
            //未登录过
            //请注册
            resolve();
        }
    })
}